package com.roncoo.education.user.service.dao.impl;

import com.alibaba.fastjson.JSON;
import com.roncoo.education.course.feign.interfaces.IFeignTag;
import com.roncoo.education.course.feign.vo.UserTagNameVO;
import com.roncoo.education.user.common.AbstractBaseJdbc;
import com.roncoo.education.user.service.dao.UserTagDao;
import com.roncoo.education.user.service.dao.impl.mapper.UserTagMapper;
import com.roncoo.education.user.service.dao.impl.mapper.entity.UserExample;
import com.roncoo.education.user.service.dao.impl.mapper.entity.UserTag;
import com.roncoo.education.util.base.Page;
import com.roncoo.education.util.base.PageUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class UserTagDaoImpl extends AbstractBaseJdbc implements UserTagDao {

    @Autowired
    private UserTagMapper userTagMapper;

    @Autowired
    private IFeignTag iFeignTag;
    /**
     * 标签用户列表
     * @param pageCurrent
     * @param pageSize
     * @param example
     * @return
     * @author wangrong
     */
    @Override
    public Page<UserTag> listForPage(int pageCurrent, int pageSize, UserExample example) {
        int count = this.userTagMapper.countByExample(example);
        pageSize = PageUtil.checkPageSize(pageSize);
        pageCurrent = PageUtil.checkPageCurrent(count, pageSize, pageCurrent);
        int totalPage = PageUtil.countTotalPage(count, pageSize);
        example.setLimitStart(PageUtil.countOffset(pageCurrent, pageSize));
        example.setPageSize(pageSize);
        return new Page<>(count, totalPage, pageCurrent, pageSize, this.userTagMapper.selectByExample(example));
    }

    @Override
    public ArrayList<UserTagNameVO> userLabel(Long userNo) {
        List<Long> tagId = userTagMapper.getUserNoByTagId(userNo);
        if(CollectionUtils.isEmpty(tagId)){
            return new ArrayList<>();
        }
        String tagIdString = JSON.toJSONString(tagId);
        return iFeignTag.userNoByTagId(tagIdString);
    }


}
